博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Webform Repeater的灵活运用
阅读量:5843 次
发布时间:2019-06-18

本文共 5742 字,大约阅读时间需要 19 分钟。

案例:模拟购物列表

封装实体类:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5  6 ///  7 /// gouwu 的摘要说明 8 ///  9 public class gouwu10 {11     public int ids { get; set; }12     public string pic { get; set; }13     public string name { get; set; }14     public decimal nowPrice { get; set; }15     public decimal oldPrice { get; set; }16     public string context { get; set; }17 18     public string qx {19         get20         {21             string eee = "";22             if (this.name == "猕猴桃")23             {24                 eee = " display:none;";25             }26 27             return eee;28         }29     }30 31 }
View Code

数据访问类:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data.SqlClient; 6  7 ///  8 /// gouwuData 的摘要说明 9 /// 10 public class gouwuData11 {12     SqlConnection conn = null;13     SqlCommand cmd = null;14     public gouwuData()15     {16         conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");17         cmd = conn.CreateCommand();18     }19 20     public List
Select()21 {22 List
glist = new List
();23 cmd.CommandText = "select *from gouwu";24 25 conn.Open();26 SqlDataReader dr = cmd.ExecuteReader();27 if (dr.HasRows)28 {29 while (dr.Read())30 {31 gouwu g = new gouwu();32 g.ids = Convert.ToInt32(dr[0]);33 g.pic = dr[1].ToString();34 g.name = dr[2].ToString();35 g.nowPrice = Convert.ToDecimal(dr[3]);36 g.oldPrice = Convert.ToDecimal(dr[4]);37 g.context = dr[5].ToString();38 39 glist.Add(g);40 41 }42 }43 44 45 conn.Close();46 47 return glist;48 }49 50 51 public void Delete(int ids)52 {53 cmd.CommandText = "delete from gouwu where ids='" + ids + "'";54 55 conn.Open();56 cmd.ExecuteNonQuery();57 conn.Close();58 }59 60 61 }
View Code

用Repeater展示:

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  2   3   4   5   6   7     
8 9 83 84 85 86
87
88
89
90
91
92
" /> 93
<%#Eval("name") %>
94
价格:<%#Eval("nowPrice") %>
<%#Eval("oldPrice") %>
95
<%#Eval("context") %>
96
97
98
99
100
101
102 103
104
105 106
界面:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class _Default : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12         if (!IsPostBack)13         {14             Repeater1.DataSource = new gouwuData().Select();15             Repeater1.DataBind();16         }17         //点击Repeater1中的按钮时发生18         Repeater1.ItemCommand += Repeater1_ItemCommand;19     }20 21     void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)22     {23         if (e.CommandName == "Delete")24         {25             new gouwuData().Delete(Convert.ToInt32(e.CommandArgument));26 27                 Repeater1.DataSource = new gouwuData().Select();28                 Repeater1.DataBind();29         }30     }31 }
后台:

不用Repeater展示:

1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 2  3  4  5  6  7     
8 9 79 80 81 82
83
84
85 86
87 88
89
90 91
92
93 94
界面
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class Default2 : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12         if (!IsPostBack)13         {14             Literal1.Text = DataBind();15         }16     }17     public string DataBind()18     {19         string end = "";20         List
glist = new gouwuData().Select();21 foreach (gouwu g in glist)22 {23 if(g.name=="香蕉")24 {25 continue;26 }27 end += "
";28 end += "
";29 end += "
" + g.name + "
";30 end += "
价格:" + g.nowPrice + "
" + g.oldPrice + "
";31 end += "
" + g.context + "
";32 end += "
删除";33 end += "
";34 }35 36 return end;37 }38 }
后台
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 public partial class Delete : System.Web.UI.Page 9 {10     protected void Page_Load(object sender, EventArgs e)11     {12         new gouwuData().Delete(Convert.ToInt32(Request["id"]));13         Response.Write("");14     }15 }
删除界面后台

 

转载于:https://www.cnblogs.com/maxin991025-/p/6261662.html

你可能感兴趣的文章
线上Linux服务器运维安全策略经验分享
查看>>
Android一些问题的解决方案
查看>>
Luogu P4707 重返现世
查看>>
目标与绩效管理实战专家胡立
查看>>
2014手机分析图
查看>>
一元多项式相加
查看>>
commandLink/commandButton/ajax backing bean action/listener method not invoked (转)
查看>>
(一) pyhon 基础语法(数值 字符串 元组 列表 字典)
查看>>
HDOJ 1003:求一串数字中和最大的连续子串
查看>>
RedHat 5.6_x86_64 + ASM + RAW+ Oracle 10g RAC (二)
查看>>
win7不能全屏
查看>>
MySQL/InnoDB的并发插入Concurrent Insert
查看>>
转两好文防丢:Debian 版本升级/降级 & Linux 应用程序失去输入焦点问题的解决...
查看>>
HDU - Pseudoforest
查看>>
Nexus杂
查看>>
Android --- GreenDao的实现(ORM框架)
查看>>
Linux平台Java调用so库-JNI使用例子
查看>>
Spring Data JPA
查看>>
项目管理修炼之道之规划项目
查看>>
Web服务器压力测试工具http_load、webbench、ab、Siege使用教程
查看>>